package com.mjh.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import org.apache.log4j.Logger;

import com.mjh.bean.ImageFileBean;
import com.mjh.comm.Constant;
import com.mjh.config.GlobalConfig;
import com.opensymphony.xwork2.ActionSupport;

public class ImageMgrAction extends ActionSupport{

	private static final long serialVersionUID = -6089133711830975045L;
	public static Logger log = Logger.getLogger(ImageMgrAction.class);
	
	private static String[] FILE_TYPES = new String[]{"gif", "jpg", "jpeg", "png", "bmp"};
	
	//接收页面的参数
	private String order;
	private String path;
	private String dirname;
	
	//返回的参数
	private String moveup_dir_path;
	private String current_dir_path;
	private String current_url;
	private String total_count;
	
	private ArrayList<ImageFileBean> file_list;
	
	//操作返回结果
	private int result;
	
	public String listFile(){
		
		String rootPath = Constant.SERVLET_CONTEXT + File.separator + GlobalConfig.getSysConfig().getImgFolder() + File.separator;
		String rootUrl  = "/" + GlobalConfig.getSysConfig().getImgFolder().replaceAll("\\\\", "/") + "/";
		
		//根据path参数，设置各路径和URL
		current_dir_path = path;
		current_url = rootUrl + path;
		moveup_dir_path = "";
		if (!"".equals(path)) {
			String str = current_dir_path.substring(0, current_dir_path.length() - 1);
			moveup_dir_path = str.lastIndexOf("/") >= 0 ? str.substring(0, str.lastIndexOf("/") + 1) : "";
		}
		
		String currentPath = rootPath + path;
		file_list = new ArrayList<ImageFileBean>();
		File fAll = new File(currentPath);
		if(fAll.isDirectory()){
			File[] fs = fAll.listFiles();
			for(File f : fs){
				ImageFileBean fileBean = new ImageFileBean();
				String fileName = f.getName();
				if(f.isDirectory()) {
					fileBean.setIs_dir(true);
					fileBean.setHas_file(f.listFiles() != null);
					fileBean.setFilesize(0L);
					fileBean.setIs_photo(false);
					fileBean.setFiletype("");
				} else if(f.isFile()){
					String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
					fileBean.setIs_dir(false);
					fileBean.setHas_file(false);
					fileBean.setFilesize(f.length());
					fileBean.setIs_photo(Arrays.<String>asList(FILE_TYPES).contains(fileExt));
					fileBean.setFiletype(fileExt);
				}
				fileBean.setFilename(fileName);
				fileBean.setDatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(f.lastModified()));
				file_list.add(fileBean);
			}
		}
		sortFile();
		return SUCCESS;
	}

	/**
	 * 创建图片文件夹
	 * @return
	 */
	public String mkDir(){
		String rootPath = Constant.SERVLET_CONTEXT + File.separator + GlobalConfig.getSysConfig().getImgFolder() + File.separator + path;
		File dir = new File(rootPath, dirname);
		try {
			dir.mkdir();
			result = Constant.SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			result = Constant.FAIL;
		}
		return SUCCESS;
	}
	
	public String delFile(){
		return SUCCESS;
	}
	
	private void sortFile(){
		Collections.sort(file_list, new Comparator<ImageFileBean>(){
			@Override
			public int compare(ImageFileBean arg0, ImageFileBean arg1) {
				if (arg0.isIs_dir() && !arg1.isIs_dir()) {
					return -1;
				} else if (!arg0.isIs_dir() && arg1.isIs_dir()) {
					return 1;
				}
				if("NAME".equals(order)){
					return arg0.getFilename().compareTo(arg1.getFilename());
				} else if("TYPE".equals(order)){
					return arg0.getFiletype().compareTo(arg1.getFiletype());
				} else if("SIZE".equals(order)){
					return (int)(arg1.getFilesize() - arg0.getFilesize());
				}
				return arg0.getFilename().compareTo(arg1.getFilename());
			}
		});
	}
	
	public String getMoveup_dir_path() {
		return moveup_dir_path;
	}

	public String getCurrent_dir_path() {
		return current_dir_path;
	}

	public String getCurrent_url() {
		return current_url;
	}

	public String getTotal_count() {
		return total_count;
	}

	public ArrayList<ImageFileBean> getFile_list() {
		return file_list;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public int getResult() {
		return result;
	}

	public String getDirname() {
		return dirname;
	}

	public void setDirname(String dirname) {
		this.dirname = dirname;
	}
}
